{{define "article.list"}}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>文章列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/statics/third-pkg/layui/css/layui.css">
    <link rel="stylesheet" href="/statics/admin/css/public.css" media="all">
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">文章标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-inline">
                                <input type="hidden" name="category_id">
                                <input type="text"  readonly="true"  name="category_name"  autocomplete="off" class="layui-input" id="categoryName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/statics/third-pkg/layui/layui.js"></script>
<script src="/statics/admin/js/lay-config.js?v=2.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'request','tableSelect'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            request = layui.request,
            tableSelect = layui.tableSelect,
            table = layui.table;
            tableSelect.render({
                elem: '#categoryName',	//定义输入框input对象 必填
                checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
                searchKey: 'name',
                searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
                table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                    url: '/admin/article-category/list-data',
                    cols: [[
                        {type: 'radio' },
                        {field: 'id',
                        width: 80,
                        title: 'ID',
                        sort: true
                    }, {
                        field: 'name',
                        width: 200,
                        title: '分类名称'
                    }, {
                        field: 'created_at',
                        width: 200,
                        title: '创建时间'
                    }]]
                },
                done: function (elem, data) {
                    if (data.isAll ==true){
                        var cate_name=data.data.map(function (item) {
                            return item.name
                        }).pop()
                        var cate_id=data.data.map(function (item) {
                            return item.id
                        }).pop()

                        $(elem).val(cate_name);
                        $(elem).prev().val(cate_id)
                    }
                }
            })

        table.render({
            elem: '#currentTableId',
            url: '/admin/article/list-data',
            toolbar: '#toolbarDemo',
            defaultToolbar: false,
            skin: 'line',
            size: 'lg',
            cols: [
                [{
                    type: "checkbox",
                    width: 50
                }, {
                    field: 'id',
                    width: 80,
                    title: 'ID',
                    sort: true
                }, {
                    field: 'title',
                    width: 100,
                    title: '标题'
                },{
                    field: 'category_name',
                    width: 150,
                    title: '分类名称'
                },{
                    field: 'status',
                    width: 100,
                    title: '状态'
                }, {
                    field: 'tags',
                    width: 200,
                    title: '标签',
                    templet:function (d) {
                        var tags_data=JSON.parse(d.tags);
                        var html=`    <div class="layui-btn-container tag">`;
                        for (let tagsNameKey in tags_data) {
                            html+=`<button type="button" class="tag-item tag-item-normal layui-btn layui-btn-primary layui-btn-sm" style="background-color:${tags_data[tagsNameKey].color};color: #ffffff">${tags_data[tagsNameKey].name}</button>`;
                        }
                        html+=`</div>`
                        return html;
                    }
                },{
                    field: 'weight',
                    width: 150,
                    title: '权重'
                } ,{
                    field: 'created_at',
                    width: 150,
                    title: '创建时间'
                },  {
                    title: '操作',
                    minWidth: 150,
                    toolbar: '#currentTableBar',
                    align: "center"
                }]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') { // 监听添加操作
                var index = layer.open({
                    title: '添加文章',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/article/add',
                    success: function (lay, index) {
                    },
                });
                return false;
            } else if (obj.event === 'delete') { // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId'),
                    data = checkStatus.data;
                layer.confirm('确定批量删除这些文章吗，个数为'+data.length, function (index) {
                    var ids=data.map(function (item){
                        return item.id
                    }).join(',')
                    if(!ids){
                        layer.msg("至少选择其中一个文章")
                    }
                    request.delete("/admin/article/batch-del?ids="+ids).then((res)=>{
                        if(res.status==204){
                            layer.msg("批量删除文章成功",{
                                time:1000
                            },function (){
                                //执行搜索重载
                                table.reload('currentTableId', {
                                    page: {
                                        curr: 1
                                    },
                                    where: data.field
                                }, 'data');
                            })
                        }else{
                            layer.msg(res.data.msg)
                        }
                    }).catch((err)=>{
                        layer.msg(err)
                    })
                });
            }
        });
        window.addEventListener("storage", function (e) {
            var item = localStorage.getItem("article");
            if(item){
                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    },
                }, 'data');
            }

        });
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑文章',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/article/edit?id=' + data.id,
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除当前文章吗', function (index) {
                    request.delete("/admin/article/del?id="+data.id).then((res)=>{
                        if(res.status==204){
                            layer.msg("删除文章成功",{
                                time:1000
                            },function (){
                                obj.del();
                            })
                        }else{
                            layer.msg(res.data.msg)
                        }
                    }).catch((err)=>{
                        layer.msg(err)
                    })

                    layer.close(index);
                });
            }
        });

    });
</script>

</body>

</html>
{{ end }}